package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataTourGroupCodeListDTO;
import com.ctshk.rpc.system.dto.MainDataTourGroupCodeQueryDTO;
import com.ctshk.rpc.system.entity.MainDataTourGroupCode;
import com.ctshk.rpc.system.enums.StopStatusCode;
import com.ctshk.rpc.system.mapper.MainDataTourGroupCodeMapper;
import com.ctshk.rpc.system.req.MainDataTourGroupCodeAddReq;
import com.ctshk.rpc.system.req.MainDataTourGroupCodeListReq;
import com.ctshk.rpc.system.req.MainDataTourGroupCodeUpdateReq;
import com.ctshk.rpc.system.req.MainDataTourGroupCodeUpdateStatusReq;
import com.ctshk.rpc.system.service.IMainDataTourGroupCodeService;
import com.ctshk.rpc.tour.service.ISeriesTripAppService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 * 旅游组别码表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-30
 */
@DubboService
public class MainDataTourGroupCodeServiceImpl extends ServiceImpl<MainDataTourGroupCodeMapper, MainDataTourGroupCode> implements IMainDataTourGroupCodeService {

    @Autowired
    private MainDataTourGroupCodeMapper mainDataTourGroupCodeMapper;

    @DubboReference
    private ISeriesTripAppService iSeriesTripAppService;
    /**
     * 新增旅游组别
     */
    @Override
    public Result add(MainDataTourGroupCodeAddReq req) {
        QueryWrapper<MainDataTourGroupCode> qw = new QueryWrapper<>();
        qw.lambda().select(MainDataTourGroupCode::getId)
                .eq(MainDataTourGroupCode::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(MainDataTourGroupCode::getTourGroupCode, req.getTourGroupCode());
        List<MainDataTourGroupCode> bools = baseMapper.selectList(qw);
        if (bools != null && bools.size() > 0) {
            return Result.failed(SystemError.MAINDATA_4027);
        }
        MainDataTourGroupCode mainDataTourGroupCode = EntityUtil.copy(req, MainDataTourGroupCode.class);
        LocalDateTime now = LocalDateTime.now();
        mainDataTourGroupCode.setGmtCreate(now);
        mainDataTourGroupCode.setGmtModified(now);
        mainDataTourGroupCode.setIsDeleted(IsDeletedCode.NO.getCode());
        long id = IdWorker.getId();
        mainDataTourGroupCode.setId(id);
        mainDataTourGroupCodeMapper.insert(mainDataTourGroupCode);

        // 更新缓存
        CompletableFuture.runAsync(() -> {
            iSeriesTripAppService.tripHomeCatalogDTO();
        });
        return Result.success(id);
    }

    /**
     * 编辑旅游组别
     */
    @Override
    public Result update(MainDataTourGroupCodeUpdateReq req) {
        MainDataTourGroupCode mainDataTourGroupCode = EntityUtil.copy(req, MainDataTourGroupCode.class);
        mainDataTourGroupCode.setGmtModified(LocalDateTime.now());
        mainDataTourGroupCodeMapper.updateById(mainDataTourGroupCode);
        // 更新缓存
        CompletableFuture.runAsync(() -> {
            iSeriesTripAppService.tripHomeCatalogDTO();
        });
        return Result.success(req.getId());
    }

    /**
     * 编辑旅游组别状态
     */
    @Override
    public Result updateStatus(MainDataTourGroupCodeUpdateStatusReq req) {
        QueryWrapper<MainDataTourGroupCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", req.getId()).eq("is_deleted", IsDeletedCode.NO.getCode());
        MainDataTourGroupCode selectOne = mainDataTourGroupCodeMapper.selectOne(queryWrapper);
        MainDataTourGroupCode mainDataTourGroupCode = EntityUtil.copy(req, MainDataTourGroupCode.class);
        if (selectOne.getStatus() == 0) {
            mainDataTourGroupCode.setStatus(1);
        } else {
            mainDataTourGroupCode.setStatus(0);
        }
        mainDataTourGroupCodeMapper.updateById(mainDataTourGroupCode);
        // 更新缓存
        CompletableFuture.runAsync(() -> {
            iSeriesTripAppService.tripHomeCatalogDTO();
        });
        return Result.success(req.getId());
    }

    /**
     * 查看旅游组别
     */
    @Override
    public Result<MainDataTourGroupCodeQueryDTO> query(MainDataTourGroupCodeUpdateStatusReq req) {
        QueryWrapper<MainDataTourGroupCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", req.getId());
        queryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
        MainDataTourGroupCode mainDataTourGroupCode = mainDataTourGroupCodeMapper.selectOne(queryWrapper);
        MainDataTourGroupCodeQueryDTO mainDataTourGroupCodeQueryDTO = EntityUtil.copy(mainDataTourGroupCode, MainDataTourGroupCodeQueryDTO.class);
        return Result.success(mainDataTourGroupCodeQueryDTO);
    }

    /**
     * 查看旅游组别
     */
    @Override
    public PageResponse<MainDataTourGroupCodeListDTO> list(MainDataTourGroupCodeListReq req) {
        Page<MainDataTourGroupCode> page = new Page<>(req.getPageNo(),req.getPageSize());
        QueryWrapper<MainDataTourGroupCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null != req.getStatus(), "status", req.getStatus());
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getName()), MainDataTourGroupCode::getTourGroupCode, req.getName())
                .or().like(StringUtils.isNotBlank(req.getName()), MainDataTourGroupCode::getTourGroupName, req.getName());
        queryWrapper.orderByDesc("gmt_modified");
        Page<MainDataTourGroupCode> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataTourGroupCode> mainDataTourGroupCodeList = iPage.getRecords();
        if (CollectionUtils.isEmpty(mainDataTourGroupCodeList)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataTourGroupCodeListDTO> mainDataTourGroupCodeListDTOS = EntityUtil.copyList(mainDataTourGroupCodeList, MainDataTourGroupCodeListDTO.class);
        return new PageResponse<>(mainDataTourGroupCodeListDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    /**
     * 旅行团首页三级分类一级菜单
     *
     * @return
     */
    @Override
    public List<MainDataTourGroupCodeQueryDTO> getMainDataTourGroupCode() {
        List<MainDataTourGroupCode> mainDataTourGroupCodes = baseMapper.selectList(Wrappers.<MainDataTourGroupCode>lambdaQuery().eq(MainDataTourGroupCode::getIsDeleted, IsDeletedCode.NO.getCode()).eq(MainDataTourGroupCode::getStatus, StopStatusCode.NO.getCode()));
        return EntityUtil.copyList(mainDataTourGroupCodes, MainDataTourGroupCodeQueryDTO.class);
    }
}
